package com.superteam.zzl.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.List;

/**
 * Created by Administrator on 2016/11/27.
 */
public abstract class MyBaseAdapter<T, V extends MyBaseAdapter.BaseHolder> extends BaseAdapter {

    private List<T> list;
    public Context context;
    private LayoutInflater inflater;
    private int resId;

    public MyBaseAdapter(Context context, List<T> list, int resId) {
        this.context = context;
        this.list = list;
        this.resId = resId;
        inflater = LayoutInflater.from(context);

    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        V v;
        if (convertView == null) {
            convertView = inflater.inflate(resId, parent, false);
            v = bindView(convertView, list, position);
            convertView.setTag(v);
        } else {
            v = (V) convertView.getTag();
        }
        bindData(v, list, position);
        return convertView;
    }

    public static class BaseHolder {

    }

    public abstract V bindView(View convertView, List<T> list, int position);
    public abstract void bindData(V v, List<T> list, int position);

}
